package com.five.controller;

import com.alibaba.fastjson.JSON;
import com.five.entity.TransactionRecords;
import com.five.entity.User;
import com.five.service.TransactionRecordService;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2018/2/23.
 */
@RestController
@RequestMapping("/youxue")
public class TransactionRecordController {

    private static final Logger logger = Logger.getLogger(TransactionRecordController.class);

    @Autowired
    private TransactionRecordService recordService;
    @Autowired
    private HttpServletRequest request;

    /**
     * 我的钱包-》交易记录-》增加
     * @param description
     * @param payAmount
     */
    @RequestMapping(value = "/transactionRecord",method = RequestMethod.GET)
    public void addTransactionRecord(@RequestParam("description") String description,
                                     @RequestParam("payAmount") Integer payAmount){
        User currentUser =
                (User) request.getSession().getAttribute("userLogin");
        TransactionRecords records = new TransactionRecords();
        records.setUserId(currentUser.getUserId());
        records.setDescription(description);
        records.setPayAmount(payAmount);
        recordService.addTransactionRecord(records);

    }

    /**
     * 我的钱包-》交易记录-》获取
     * @return
     */
    @RequestMapping(value = "/transactionRecords",method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
    public String transactionRecords(){
        User currentUser =
                (User) request.getSession().getAttribute("userLogin");
        logger.info(currentUser.getUserId());
        List<TransactionRecords> recordss;
        try {
            recordss = recordService.getRecords(currentUser.getUserId());
            logger.info(recordss.size());
        }catch (NullPointerException e){
            logger.info(e.getCause());
            return "";
        }

        return JSON.toJSON(recordss).toString();
    }





}
